//
//  MYLoginBaseTextFieldPhoneCell.swift
//  MuYing
//
//  Created by tbb on 2019/7/19.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit


class MYLoginBaseTextFieldPhoneCell: MYLoginBaseTextFieldCell {


    @objc lazy var leftLb:UILabel = {
        let leftLb = UILabel();
        leftLb.textColor = appMainTitleColor;
        leftLb.font = UIFont.systemFont(ofSize: 15, weight: .medium);
        leftLb.text = "+852";
        return leftLb;
    }()
    
    lazy var moreIcon: UIImageView = {
        let moreIcon = UIImageView(image: UIImage(named: "Login_more_select_icon"));
        moreIcon.isUserInteractionEnabled = true;
        return moreIcon
    }()
    
    @objc lazy var leftBgView: UIView = {
        let leftView = UIView();
        return leftView;
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews();
        
        self.leftLb.sizeToFit();
        let lbw = self.leftLb.width;
        let leftViewW = lbw + 45;
        self.leftBgView.height = 21;
        
        self.leftLb.frame = CGRect(x: 0, y: 0, width: lbw, height: 21);
        self.moreIcon.frame = CGRect(x: lbw+7, y: 0, width: 21, height: 21);
        self.leftBgView.addSubview(self.leftLb);
        self.leftBgView.addSubview(self.moreIcon);
        
        self.leftBgView.width = leftViewW;
        
        self.textField.leftView = self.leftBgView;
        self.textField.leftViewMode = .always;
        self.textField.addTarget(self, action:Selector(("textFieldDidChange:")), for: UIControl.Event.editingChanged)

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        guard let _ = textField.text else { return }
        
        debugPrint("\(textField.text!)")
        let lang = textField.textInputMode?.primaryLanguage
        let tmpText = textField.text!
        if lang == "zh-Hans" {
            let range = textField.markedTextRange
            if range == nil {
                guard tmpText.count >= 20 else { return }
                textField.text = tmpText.mySubString(to: 20)
            }
        } else {
            guard tmpText.count >= 20 else { return }
            textField.text = tmpText.mySubString(to: 20)
        }
    }

}

//一旦写完，所有的其他地方都可以直接调用限制文本输入长度
extension String {
    func mySubString(to index: Int) -> String {
        return String(self[..<self.index(self.startIndex, offsetBy: index)])
    }
    
    func mySubString(from index: Int) -> String {
        return String(self[self.index(self.startIndex, offsetBy: index)...])
    }
    
    func toRange(_ range: NSRange) -> Range<String.Index>? {
        guard let from16 = utf16.index(utf16.startIndex, offsetBy: range.location, limitedBy: utf16.endIndex) else { return nil }
        guard let to16 = utf16.index(from16, offsetBy: range.length, limitedBy: utf16.endIndex) else { return nil }
        guard let from = String.Index(from16, within: self) else { return nil }
        guard let to = String.Index(to16, within: self) else { return nil }
        return from ..< to
    }
    
}
